const {ElMessage} = require("element-plus");

/**
 * 将字符串数字加1后再返回字符串
 * @param theStr
 * @returns {string}
 */
function strAddOne(theStr) {
    let num = parseInt(theStr);
    num += 1;
    let newStr = num.toString();
    return newStr;
}

/**
 * 将字符串数字减1后再返回字符串
 * @param theStr
 * @returns {string}
 */
function strSubOne(theStr) {
    let num = parseInt(theStr);
    num -= 1;
    let newStr = num.toString();
    return newStr;
}

function changeRelList(theList, argIdName, argCountName , argNowUserCountName, theId) {
    const theData = theList.find(obj => obj[argIdName] === theId);
    /* 若为1，则说明已经点赞了，需要取消点赞，置为0 */
    /* 若为0，则说明还没点赞，需要点赞，置为1 */
    console.log(theData);
    if (theData[argNowUserCountName] === "0") {
        console.log(theData);
        theData[argNowUserCountName] = "1";
        theData[argCountName] = strAddOne( theData[argCountName] )
    }
    else if ( theData[argNowUserCountName] !== "0" ) {
        theData[argNowUserCountName] = "0";
        theData[argCountName] = strSubOne( theData[argCountName] )
    }

}

function successedOutVOHandle(successedOutVO) {
    if (successedOutVO.successed === "1") {
        ElMessage({
            showClose: true,
            message: successedOutVO.message,
            type: 'success',
            center: true,
        });
    }
    else if (successedOutVO.successed === "0") {
        ElMessage({
            showClose: true,
            message: successedOutVO.message,
            type: 'error',
            center: true,
        });
    }
}

module.exports = {strAddOne, strSubOne, changeRelList, successedOutVOHandle};
